

import { getFileList, getviewList, getFileEdit, delFileEdit, searFile, searview, delview } from '@/services';
import { fileList, viewList } from '@/types';
import { message } from 'antd';
import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';

export interface viewModelState {
    viewList: viewList[],
    viewLength: number,
    fileList: fileList[],
    flileLength: number,
    fileEdit: Partial<fileList>
}

export interface viewModelType {
    namespace: 'view';
    state: viewModelState;
    effects: {
        getviewList: Effect;
        getFileList: Effect;
        getFileEdit: Effect;
        delFileEdit: Effect;
        searFile: Effect;
        searview: Effect;
        delview: Effect
    };
    reducers: {
        save: Reducer<viewModelState>;
        // 启用 immer 之后
        // save: ImmerReducer<viewModelState>;
    }
}

const viewModel: viewModelType = {
    // namespace： 命名空间 数据模型，限定模块之间不相互影响 
    namespace: 'view',
    // state：数据模型状态，相当于vuex的state
    state: {
        viewList: [],
        viewLength: 0,
        fileList: [],
        flileLength: 0,
        fileEdit: {}
    },
    // effects：异步操作，相当于vuex的action
    effects: {
        // * 相当于async, yield 相当于await
        *getviewList({ payload }, { call, put }) {
            let result = yield call(getviewList, payload)
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        viewList: result.data[0],
                        viewLength: result.data[1]
                    }
                })
            }

        }, //查询用户  delview
        *searview({ payload }, { call, put }) {
            const { IP, address, browser, device, engine, os, url, userAgent } = payload
            let result = yield call(searview, IP, address, browser, device, engine, os, url, userAgent)
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        viewList: result.data[0],
                        viewLength: result.data[1]
                    }
                })
            }

        },
        // 删除用户
        *delview({ payload }, { call, put }) {
            let result = yield call(delview, payload.key)
            if (result.statusCode == 200) {
                let result = yield call(getviewList, payload.page)
                if (result.data) {
                    yield put({
                        type: "save",
                        payload: {
                            viewList: result.data[0],
                            viewLength: result.data[1]
                        }
                    })
                }
            }
        },
        //获取所有文件  
        *getFileList({ payload }, { call, put }) {
            let result = yield call(getFileList, payload)
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        fileList: result.data[0],
                        flileLength: result.data[1]
                    }
                })
            }
        },  //获取文件详情
        *getFileEdit({ payload }, { call, put }) {
            let result = yield call(getFileEdit, payload)
            console.log(result)
            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        fileEdit: result.data,
                    }
                })
            }
        }, //删除文件
        *delFileEdit({ payload }, { call, put }) {
            // console.log(payload,"9999")
            let result = yield call(delFileEdit, payload.id)
            if (result.statusCode == 200) {
                message.success('删除成功')
                let result = yield call(getFileList, payload.page)
                if (result.data) {
                    yield put({
                        type: "save",
                        payload: {
                            fileList: result.data[0],
                            flileLength: result.data[1]
                        }
                    })
                }
            }
        }, //搜索文件
        *searFile({ payload }, { call, put }) {

            const { originalname, type } = payload
            let result = yield call(searFile, originalname, type)

            if (result.data) {
                yield put({
                    type: "save",
                    payload: {
                        fileList: result.data[0],
                        viewLength: result.data[1]
                    }
                })
            }
        }
    },
    // reducers：同步操作，相当于vuex的mutation   
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        }
    },
};

export default viewModel;